﻿Imports ControllerSigeco

Public Class frmMercaderiaListado

    Private Sub frmMercaderiaListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgMercaderia.AutoGenerateColumns = False
        CargarCombos()
        CargarMercaderias()
    End Sub

    Private m_frmMercaderias As frmMercaderia
    Public Property frmMercaderias() As frmMercaderia
        Get
            Return m_frmMercaderias
        End Get
        Set(ByVal value As frmMercaderia)
            m_frmMercaderias = value
        End Set
    End Property

    Dim classController As New controller_stock

    Private _idMercaderia As Integer = 0

    Public Property idMercaderia() As Integer
        Get
            Return _idMercaderia
        End Get
        Set(value As Integer)
            _idMercaderia = value
        End Set
    End Property

    Private Sub CargarMercaderias()
        Try
            idMercaderia = 0
            txtMercaderia.Focus()

            btnBuscar.Enabled = True
            btnNuevo.Enabled = True
            btnEliminar.Enabled = True

            Dim idMarca As Integer = 0
            If cbxMarca.Checked Then
                idMarca = cboMarca.SelectedValue
            End If

            Dim idRubro As Integer = 0
            If cbxRubro.Checked Then
                idRubro = cboRubro.SelectedValue
            End If

            Dim ds As New DataTable
            ds = classController.get_mercaderias_All(oiml.dbConn, 0, txtMercaderia.Text, idMarca, idRubro, False, txtCodigoBarra.Text, 0, 0)

            If Not ds Is Nothing Then
                dgMercaderia.DataSource = ds
            Else
                dgMercaderia.DataSource = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub CargarCombos()
        Dim dsMarca As New DataTable
        dsMarca = classController.get_marcas(oiml.dbConn, 0, "")
        If Not dsMarca Is Nothing Then
            cboMarca.DataSource = dsMarca
            cboMarca.DisplayMember = "marca"
            cboMarca.ValueMember = "idMarca"
        End If

        Dim dsRubro As New DataTable
        dsRubro = classController.get_rubros(oiml.dbConn, 0, "")
        If Not dsRubro Is Nothing Then
            cboRubro.DataSource = dsRubro
            cboRubro.DisplayMember = "rubro"
            cboRubro.ValueMember = "idRubro"
        End If

        'Dim dsImpuesto As New DataTable
        'dsImpuesto = classController.get_impuestos(oiml.dbConn, 0, "")
        'If Not dsImpuesto Is Nothing Then
        '    cboImpuesto.DataSource = dsImpuesto
        '    cboImpuesto.DisplayMember = "impuesto"
        '    cboImpuesto.ValueMember = "idImpuesto"
        'End If

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs)

        If idMercaderia > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Mercaderias", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_mercaderia(oiml.dbConn, idMercaderia) Then
                    txtMercaderia.Text = ""
                    CargarMercaderias()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub editar()
        Try

            If dgMercaderia.SelectedRows.Count > 0 Then

                idMercaderia = dgMercaderia.SelectedRows(0).Cells(0).Value
                If idMercaderia > 0 Then
                    frmMercaderias = New frmMercaderia()
                    frmMercaderias.idMercaderia = idMercaderia
                    frmMercaderias.ShowDialog()
                    CargarMercaderias()
                End If

            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        frmMercaderias = New frmMercaderia()
        frmMercaderias.idMercaderia = 0
        frmMercaderias.ShowDialog()
        CargarMercaderias()

    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        CargarMercaderias()

    End Sub

    Private Sub dgMercaderia_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            editar()
        End If
    End Sub

    Private Sub dgMercaderia_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgMercaderia.DoubleClick
        editar()
    End Sub

    Private Sub txtMarca_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMercaderias()
        End If
    End Sub

    Private Sub dgMercaderia_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgMercaderia.CellClick
        Try

            If dgMercaderia.SelectedRows.Count > 0 Then

                idMercaderia = dgMercaderia.SelectedRows(0).Cells(0).Value


            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class